package com.ruoyi.model.manager.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.model.manager.domain.ColumnNode;
import com.ruoyi.model.manager.domain.TableNode;
import com.ruoyi.model.manager.mapper.TableMapper;
import com.ruoyi.model.manager.service.ITableService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

@Service
public class TableServiceImpl implements ITableService {


    @Resource
    private TableMapper tableMapper;

    /**
     * 根据条件获取表名
     *
     * @param condition 条件表达式
     * @return 符号条件的表名
     */
    @Override
    @DataSource(DataSourceType.SLAVE)
    public  List<TableNode> getTableTree(String condition) {
        List<Map<String, Object>> result = tableMapper.getTableColumns(condition);

        // 使用 Map 来存储表名和对应的列
        Map<String, TableNode> tableMap = new HashMap<>();

        for (Map<String, Object> row : result) {
            String tableName = (String) row.get("tableName");
            String columnName = (String) row.get("columnName");

            TableNode tableNode = tableMap.computeIfAbsent(tableName, k -> new TableNode());
            tableNode.setName(tableName);

            ColumnNode columnNode = new ColumnNode();
            columnNode.setName(tableName+"."+columnName);

            if (tableNode.getColumns() == null) {
                tableNode.setColumns(new ArrayList<>());
            }
            tableNode.getColumns().add(columnNode);
        }

        return new ArrayList<>(tableMap.values());
    }

    /**
     * 获取表字段信息数据
     *
     * @param tables
     * @return
     */
    @Override
    public List<JSONObject> getTableColumnsData(List<TableNode> tables,int limit) {
        List<CompletableFuture<JSONObject>> futures = new ArrayList<>();

        tables.forEach(table->{
            CompletableFuture<JSONObject> future = CompletableFuture.supplyAsync(() -> {
                JSONObject node = new JSONObject();
                node.put("tableName", table.getName());
                node.put("columns", table.getColumns());
                node.put("data", this.tableMapper.getTableColumnsData(table, limit));
                return node;
            });
            futures.add(future);
        });

        return futures.stream()
                .map(CompletableFuture::join)
                .collect(Collectors.toList());
//        List<JSONObject> result = new ArrayList<>();

//        tables.forEach(table->{
//            JSONObject node = new JSONObject();
//            node.put("tableName", table.getTableName());
//            node.put("columns", table.getColumns());
//            node.put("data", this.tableMapper.getTableColumnsData(table));
//            result.add(node);
//        });
//        return result;
    }

}
